package com.lai.service;

import com.lai.dao.UserMapper;
import com.lai.dao.WalletMapper;
import com.lai.pojo.Transaction;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
    @Autowired
    private WalletMapper walletMapper;

    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public BigDecimal getBalance(int userId) {
        return walletMapper.selectBalanceById(userId);
    }

    public void spend(@Param("userId")int userId, @Param("amount")BigDecimal amount) {
        walletMapper.spendFromWallet(userId,amount);
    }

    public void refund(@Param("userId")int userId, @Param("amount")BigDecimal amount) {
        walletMapper.refundToWallet(userId,amount);
    }

    public List<Transaction> selectTransaction(int userId) {
        return walletMapper.selectTransaction(userId);
    }
}
